/**
	Copyright (c) 2012 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 4/3/2012
*/

public without sharing class MC2PageController {
	private Survey_Form__c f;
	private string selectedRecordTypeId, accountId, retUrl, fId;
	private string precallContactRecordTypeId = RecordTypeHelper.GetRecordTypeIdByDevName('Survey_Form__c', 'Pre_Call_Customer_Contact'); 
	private string firstContactRecordTypeId   = RecordTypeHelper.GetRecordTypeIdByDevName('Survey_Form__c', 'First_Customer_Contact'); 
	private string secondContactRecordTypeId  = RecordTypeHelper.GetRecordTypeIdByDevName('Survey_Form__c', 'Second_Customer_Contact');
	public Boolean isError {set;get;}
	public String label_pre1  {set;get;}
	public String label_pre2a {set;get;}
	public String label_pre2b {set;get;}
	public String label_pre3  {set;get;}
	
	public String label_first1   {set;get;}
	public String label_first1a  {set;get;}
	public String label_first1b  {set;get;}
	public String label_first1c  {set;get;}
	public String label_first2   {set;get;}
	public String label_first3a  {set;get;}
	public String label_first3b  {set;get;}
	public String label_first4   {set;get;}
	public String label_first5   {set;get;}
	public String label_first6   {set;get;}
	
	public String label_second1   {set;get;}
	public String label_second2   {set;get;}
	public String label_second3   {set;get;}
	public String label_second4   {set;get;}
	public String label_second5   {set;get;}
	public String label_second6   {set;get;}
	public String label_second7   {set;get;}
	public String label_second7a  {set;get;}
	public String label_second8   {set;get;}
	public String label_second9   {set;get;}
	public String label_second10  {set;get;}
	public String label_second11  {set;get;}
	public String label_second12  {set;get;}
	public String label_second13  {set;get;}
	public String label_second14  {set;get;}
	
	public MC2PageController(ApexPages.StandardController stdController){
		this.isError = false;
		fId = ApexPages.currentPage().getParameters().get('id');
		selectedRecordTypeId  = ApexPages.currentPage().getParameters().get('RecordType');
		retUrl = ApexPages.currentPage().getParameters().get('retURL');
		accountId = ApexPages.currentPage().getParameters().get('accid');
		if(accountId == null){
			if(fId == null){
				InitNewStep1();
			}else{
				InitRedirectEditOrDetail();
			}
		}else{
			InitNewStep2(stdController);
		}
		if(this.f.RecordTypeId == precallContactRecordTypeId){
			InitializePreCallForm();
		}else if(this.f.RecordTypeId == firstContactRecordTypeId){
			InitializeFirstForm();
		}else if(this.f.RecordTypeId == secondContactRecordTypeId){
			InitializeSecondForm();
		}
	}
	
	private void InitNewStep1(){
		String t_retUrl = retUrl;
        if(accountId==null && t_retUrl != null && t_retUrl.startsWith('/001')){
        	Integer  parameterPosition =  t_retUrl.indexOf('?');   
        	if(parameterPosition > 0){
        		t_retUrl = t_retUrl.substring(0,parameterPosition);
            }
            accountId= EncodingUtil.urlDecode(t_retUrl,'UTF-8').replaceAll('/','');
        }		
	}
		
	private void InitNewStep2(ApexPages.StandardController stdController){
		this.f = (Survey_Form__c) stdController.getRecord();
        this.f.Account__c = accountId;
        this.f.RecordTypeId = selectedRecordTypeId;
        Account a = [select CSS_CustomerID__c from Account where id =: this.f.Account__c limit 1];
        this.f.CSS_Customer_ID__c = a.CSS_CustomerID__c;
        User u = [select SFA_Office__c, ATTUID__c from User where id =: UserInfo.getUserId() limit 1];
        this.f.Office__c = u.SFA_Office__c;
        this.f.Employee_ATTUID__c = u.ATTUID__c;
        this.f.P2R_link_in_Pre_Call__c = 'https://p2r.att.com/login/login.aspx';
        this.f.P2R_link_in_1st_Contact__c = 'https://p2r.att.com/login/login.aspx';	
	}

	private void InitRedirectEditOrDetail(){
		this.f = [select RecordTypeId, Account__c from Survey_Form__c where id =: fId limit 1];
	}
	
	private String checkIfMoreThanOneRecord(){
		String errMessage;
		List<Survey_Form__c> forms = [select id, RecordType.Name from Survey_Form__c 
									  where Account__c =: accountId and RecordTypeId =: selectedRecordTypeId order by LastModifiedDate desc];
		if(forms.size() > 0){
			this.isError = true;
			errMessage = ' You can\'t create more than one "' + forms[0].RecordType.Name + '" survey form. Please click "Ok" to go back to the account and edit an existing survey form.';
		}else{
			this.isError = false;
		}
		return errMessage;
	}
				
	public PageReference doRedirect() {
		if(fId == null){ // create new record
			String errMsg = checkIfMoreThanOneRecord();
			if(!this.isError){ // only one record is allowed for one record type per one account.
				PageReference newPage;
				if(precallContactRecordTypeId.contains(selectedRecordTypeId)){
		            newPage = Page.MC2precallpage;
		        }else if(firstContactRecordTypeId.contains(selectedRecordTypeId)){
		            newPage = Page.MC2firstContactpage;
		        }else if(secondContactRecordTypeId.contains(selectedRecordTypeId)){
		            newPage = Page.MC2secondContactpage;
		        }
		        if(newPage != null){
			        newPage.setRedirect(true);
			        newPage.getParameters().put('retURL',retUrl);
			        newPage.getParameters().put('RecordType',selectedRecordTypeId );
			        if(accountId != null){
			        	newPage.getParameters().put('accid',accountId );
			        }
			        return newPage;
		        }
			}else{
				addError(errMsg);
				return null;
			}
		}else{ // update or read existing record
			if(retUrl != null){ // edit mode
				PageReference editPage;
				if(precallContactRecordTypeId.contains(this.f.RecordTypeId))   {
		            editPage = Page.MC2precallpage;
				}else if(firstContactRecordTypeId.contains(this.f.RecordTypeId))   {
		            editPage = Page.MC2firstContactpage;
				}else if(secondContactRecordTypeId.contains(this.f.RecordTypeId)){
		            editPage = Page.MC2secondContactpage;
				}
				if(editPage != null){
		            editPage.setRedirect(true);
		            editPage.getParameters().put('id',fId);
		            editPage.getParameters().put('retURL',retUrl);
		            return editPage;
				}	
			}else{ // read mode
				PageReference readPage;
				if(precallContactRecordTypeId.contains(this.f.RecordTypeId))   {
		            readPage = Page.MC2precallDetailpage;
				}else if(firstContactRecordTypeId.contains(this.f.RecordTypeId))   {
		            readPage = Page.MC2firstContactDetailpage;			
				}else if(secondContactRecordTypeId.contains(this.f.RecordTypeId)){
		            readPage = Page.MC2secondContactDetailpage;
				}
				if(readPage != null){
					readPage.setRedirect(true);
		            readPage.getParameters().put('id',fId);
		            return readPage;	
				}
			}
		}
		return null;
	}
	
	public PageReference deleted() {
		if(fId != null){
			Survey_Form__c del_f = [select id from Survey_Form__c where id =: fId limit 1];
			delete f;
			PageReference accounthomepage = new PageReference('/001/o');
			accounthomepage.setRedirect(true);
			return accounthomepage;
		}
		return null;
	}
	
	private void InitializePreCallForm(){
		InitializeFieldLabelsPreCallForm();
	}
		
	private void InitializeFirstForm(){
		InitializeFieldLabelsFirstForm();
	}
	
	private void InitializeSecondForm(){
		InitializeFieldLabelsSecondForm();
	}
	
	private void InitializeFieldLabelsPreCallForm(){
		this.label_pre1  = '1. '   + Survey_Form__c.Pre_call_identifcation_of_issue__c.getDescribe().getLabel();
		this.label_pre2a = '2.a. ' + Survey_Form__c.Problem_in_Pre_Call__c.getDescribe().getLabel();
		this.label_pre2b = '2.b. ' + Survey_Form__c.Outline_issue_in_Pre_Call__c.getDescribe().getLabel();
		this.label_pre3  = '3. '   + Survey_Form__c.Date_of_appointment__c.getDescribe().getLabel();
	}
	
	private void InitializeFieldLabelsFirstForm(){
		this.label_first1  = '1. '   + Survey_Form__c.Has_solution_of_issue_before_contact__c.getDescribe().getLabel();
		this.label_first1a = '1.a. ' + Survey_Form__c.Status__c.getDescribe().getLabel();
		this.label_first1b = '1.b. ' + Survey_Form__c.Is_customer_satisfied__c.getDescribe().getLabel();
		this.label_first1c = '1.c. ' + Survey_Form__c.Reason__c.getDescribe().getLabel();
		this.label_first2  = '2. '   + Survey_Form__c.Identified_a_Problem_1st_Contact__c.getDescribe().getLabel();
		this.label_first3a = '3.a. ' + Survey_Form__c.Problem_in_1st_Contact__c.getDescribe().getLabel();
		this.label_first3b = '3.b. ' + Survey_Form__c.Outline_issue_in_1st_Contact__c.getDescribe().getLabel();
		this.label_first4  = '4. '   + Survey_Form__c.Date_of_next_appointment__c.getDescribe().getLabel();
		this.label_first5  = '5. '   + Survey_Form__c.Good_for_lead_targeted_program__c.getDescribe().getLabel();
		this.label_first6  = '6. '   + Survey_Form__c.Option_s__c.getDescribe().getLabel();	
	}
	
	private void InitializeFieldLabelsSecondForm(){
		this.label_second1  = '1. '   + Survey_Form__c.BOTS__c.getDescribe().getLabel();
		this.label_second2  = '2. '   + Survey_Form__c.Initial_Forecast__c.getDescribe().getLabel();
		this.label_second3  = '3. '   + Survey_Form__c.Good_for_lead_targeted_program__c.getDescribe().getLabel();
		this.label_second4  = '4. '   + Survey_Form__c.Option_s__c.getDescribe().getLabel();
		this.label_second5  = '5. '   + Survey_Form__c.Explained_lead_targeted_program__c.getDescribe().getLabel();
		this.label_second6  = '6. '   + Survey_Form__c.Directory__c.getDescribe().getLabel();
		this.label_second7  = '7. '   + Survey_Form__c.Main_Heading__c.getDescribe().getLabel();
		this.label_second7a = '7.a. ' + Survey_Form__c.Please_Specify_Industry__c.getDescribe().getLabel();
		this.label_second8  = '8. '   + Survey_Form__c.Average_new_customer_worth__c.getDescribe().getLabel();
		this.label_second9  = '9. '   + Survey_Form__c.How_many_new_customers_can_you_handle__c.getDescribe().getLabel();
		this.label_second10 = '10. '  + Survey_Form__c.Estimated_conversion__c.getDescribe().getLabel();
		this.label_second11 = '11. '  + Survey_Form__c.Number_of_leads_would_have_from_AT_T__c.getDescribe().getLabel();
		this.label_second12 = '12. '  + Survey_Form__c.Notes_comments__c.getDescribe().getLabel();
		this.label_second13 = '13. '  + Survey_Form__c.P2R_escalation_is_opened_in_Pre_Call__c.getDescribe().getLabel();
		this.label_second14 = '14. '  + Survey_Form__c.When_the_issue_is_resolved__c.getDescribe().getLabel();		
	}

    //    Method adds an Error message to the page
    private void addError(String errorMsg){         
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,errorMsg);               
        ApexPages.addMessage(msg);
    } 
        
    //    Method adds an Warning message to the page
    private void addWarning(String warningMsg){         
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.WARNING,warningMsg);               
        ApexPages.addMessage(msg);
    } 	
}